local zhuanfeng = fk.CreateSkill{
  name = "xiaobai__zhuanfeng",
}

Fk:loadTranslationTable{
  ["xiaobai__zhuanfeng"] = "转锋",
  [":xiaobai__zhuanfeng"] = "出牌阶段开始时，你可选择获得或交给攻击范围内所有角色各一张牌。本阶段你下次造成伤害后执行另一项。",
  ["#xiaobai__zhuanfeng_obtain"] = "获得",
  ["#xiaobai__zhuanfeng_give"] = "交给",
  ["#xiaobai__zhuanfeng_prompt"] = "转锋：你可以选择获得或交给攻击范围内所有角色各一张牌",
  ["#xiaobai__zhuanfeng_obtain_prompt"] = "转锋：选择要获得的牌",
  ["#xiaobai__zhuanfeng_give_prompt"] = "转锋：选择要交给的牌",

  ["$xiaobai__zhuanfeng1"] = "焚素服而披犀甲，裂海岱而屏东国。",
  ["$xiaobai__zhuanfeng2"] = "修德者若素丝行荆棘；仁义士作豺狼齿间糜。"
}


zhuanfeng:addEffect(fk.EventPhaseStart, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return event == fk.EventPhaseStart and target == player and player:hasSkill(self) and player.phase == Player.Play
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local choice = room:askToChoice(player, {
      choices = {
        "#xiaobai__zhuanfeng_obtain",
        "#xiaobai__zhuanfeng_give",
        "Cancel"
      },
      cancelable = true,
      skill_name = zhuanfeng.name,
      prompt = "#xiaobai__zhuanfeng_prompt"
    })
    if choice~="Cancel" then
      event:setCostData(self, {choice = choice})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local choice = event:getCostData(self).choice
    local room = player.room
    local targets = table.filter(room.alive_players, function (p)
      return p ~= player and player:inMyAttackRange(p)
    end)
    
    if choice == "#xiaobai__zhuanfeng_obtain" then
      room:notifySkillInvoked(player, zhuanfeng.name, "control")
      player:broadcastSkillInvoke(zhuanfeng.name, 1)
      -- 获得攻击范围内所有角色各一张牌
      for _, p in ipairs(targets) do
        if p:getHandcardNum() > 0 then
          local card_ids = room:askToChooseCards(player, {
            max = 1,
            min = 1,
            target = p,
            flag = "h",
            prompt = "#xiaobai__zhuanfeng_obtain_prompt",
            skill_name = zhuanfeng.name
          })
          if #card_ids > 0 then
            room:obtainCard(player, card_ids[1], false, fk.ReasonJustMove)
          end
        end
      end
      -- 标记下次造成伤害后执行另一项
      player:setMark("xiaobai__zhuanfeng_give-phase", 1)
    elseif choice == "#xiaobai__zhuanfeng_give" then
      -- 交给攻击范围内所有角色各一张牌
      room:notifySkillInvoked(player, zhuanfeng.name, "control")
      player:broadcastSkillInvoke(zhuanfeng.name, 2)
      for _, p in ipairs(targets) do
        if player:getHandcardNum() > 0 then
          local card_ids = room:askToChooseCards(player, {
            max = 1,
            min = 1,
            flag = "h",
            target =player,
            prompt = "#xiaobai__zhuanfeng_give_prompt",
            skill_name = zhuanfeng.name
          })
          if #card_ids > 0 then
            room:obtainCard(p, card_ids[1], false, fk.ReasonJustMove)
          end
        end
      end
      -- 标记下次造成伤害后执行另一项
      player:setMark("xiaobai__zhuanfeng_obtain-phase", 1)
    end
  end
})

-- 处理造成伤害后的效果
local damage_spec = {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill("xiaobai__zhuanfeng") and 
           (player:getMark("xiaobai__zhuanfeng_obtain-phase") > 0 or player:getMark("xiaobai__zhuanfeng_give-phase") > 0)
  end,
  on_cost = function (self, event, target, player, data)
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function (p)
      return p ~= player and player:inMyAttackRange(p)
    end)
    
    if player:getMark("xiaobai__zhuanfeng_obtain-phase") > 0 then
      room:notifySkillInvoked(player, zhuanfeng.name, "control")
      player:broadcastSkillInvoke(zhuanfeng.name, 1)
      for _, p in ipairs(targets) do
        if p:getHandcardNum() > 0 then
          local card_ids = room:askToChooseCards(player, {
            max = 1,
            min = 1,
            target= p,
            flag = "h",
            prompt = "#xiaobai__zhuanfeng_obtain_prompt",
            skill_name = zhuanfeng.name
          })
          if #card_ids > 0 then
            room:obtainCard(player, card_ids[1], false, fk.ReasonJustMove)
          end
        end
      end
      room:setPlayerMark(player, "xiaobai__zhuanfeng_obtain-phase", 0)
    elseif player:getMark("xiaobai__zhuanfeng_give-phase") > 0 then
      room:notifySkillInvoked(player, zhuanfeng.name, "control")
      player:broadcastSkillInvoke(zhuanfeng.name, 2)
      for _, p in ipairs(targets) do
        if player:getHandcardNum() > 0 then
          local card_ids = room:askToChooseCards(player, {
            max = 1,
            min = 1,
            flag = "h",
            target = player,
            prompt = "#xiaobai__zhuanfeng_give_prompt",
            skill_name = zhuanfeng.name
          })
          if #card_ids > 0 then
            room:obtainCard(p, card_ids[1], false, fk.ReasonJustMove)
          end
        end
      end
      room:setPlayerMark(player, "xiaobai__zhuanfeng_give-phase", 0)
    end
  end
}

zhuanfeng:addEffect(fk.Damage, damage_spec)

return zhuanfeng 